package cn.swu.lyx.homework4.ui.notifications

import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ListView
import android.widget.TextView
import androidx.fragment.app.Fragment
import  cn.swu.lyx.homework4.R
import com.google.gson.Gson
import okhttp3.Call
import okhttp3.Callback
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import java.io.IOException

class NotificationsFragment : Fragment() {

    var newsList: List<News> = ArrayList()
    var newsHandler: NewsHandler? = null
    var newsAdapter: NewsAdapter? = null
    var newsListView: ListView? = null
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_notifications, null)
        newsListView = view!!.findViewById<View>(R.id.newsListView) as ListView
        newsHandler = NewsHandler()
        newsAdapter =NewsAdapter()
        RequestNewsDataByOkHttp()
        return view
    }

    fun RequestNewsDataByOkHttp() {
        val client = OkHttpClient()
        val request: Request = Request.Builder().url(NewsUrl).build()
        client.newCall(request).enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {

                e.printStackTrace()
            }

            @Throws(IOException::class)
            override fun onResponse(call: Call, response: Response) {
                val newsJson: String? = response.body?.string()
                val newsData: RequestNewsData =
                    Gson().fromJson(newsJson, RequestNewsData::class.java)
                val message = Message()
                message.what = 0
                message.obj = newsData
                newsHandler?.sendMessage(message)
            }
        })
    }

    inner class NewsHandler : Handler() {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            if (msg.what == 0) {
                newsList = msg.obj as List<News>
                newsListView!!.adapter = newsAdapter
            }
        }
    }

    inner class NewsAdapter : BaseAdapter() {
        override fun getCount(): Int {
            return newsList.size
        }

        override fun getItem(position: Int): Any {
            return newsList[position]
        }

        override fun getItemId(position: Int): Long {
            return position.toLong()
        }

        override fun getView(position: Int, convertView: View, parent: ViewGroup): View {
            var convertView = convertView
            val titleText: TextView
            val news = getItem(position) as News
            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.news_item, null)
                titleText = convertView.findViewById<View>(R.id.newsTitleText) as TextView
                convertView.tag = titleText
            } else {
                titleText = convertView.tag as TextView
            }
            titleText.setText(news.title)
            return convertView
        }
    }

    companion object {
        private const val NewsUrl =
            "https://way.jd.com/jisuapi/get?channel=头条&num=15&start=0&appkey=0d06c61b38f391b2ff3aeaad09bf8661"
    }
}


